package com.example.wanandroidsdk;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.wanandroidsdk.bean.BannerBean;
import com.example.wanandroidsdk.bean.HotKeyBean;
import com.example.wanandroidsdk.http.WanHttpRequest;
import com.example.wanandroidsdk.http.WanHttpRequestCallBack;
import com.example.wanandroidsdk.utils.LogUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "WanHttp";
    private TextView tvTest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        tvTest = findViewById(R.id.tvTest);
        tvTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WanHttpRequest.getBanner(new WanHttpRequestCallBack() {
                    @Override
                    public Boolean onResponse(String bannerBeans) {
                        try {
                            JSONObject jsonObject = new JSONObject(bannerBeans);
                            List<BannerBean> list = new ArrayList<>();
                            JSONArray array = jsonObject.getJSONArray("data");
                            if (array != null && array.length() > 0) {
                                for (int i = 0; i < array.length(); i++) {
                                    BannerBean bannerBean = new BannerBean();
                                    JSONObject object = array.getJSONObject(i);
                                    bannerBean.setId(object.getInt("id"));
                                    bannerBean.setUrl(object.getString("url"));
                                    bannerBean.setDesc(object.getString("desc"));
                                    bannerBean.setImagePath(object.getString("imagePath"));
                                    bannerBean.setIsVisible(object.getInt("isVisible"));
                                    bannerBean.setOrder(object.getInt("order"));
                                    bannerBean.setTitle(object.getString("title"));
                                    bannerBean.setType(object.getInt("type"));
                                    list.add(bannerBean);
                                }
                                LogUtils.d(TAG, list.get(0).getTitle());
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        return null;
                    }

                    @Override
                    public Boolean onFailed(String message) {
                        LogUtils.d(TAG, message);
                        return null;
                    }
                } );
            }
        });
        getData();
    }

    private void getData() {
        WanHttpRequest.getHotKey(new WanHttpRequestCallBack() {
            @Override
            public Boolean onResponse(String Data) {
                try {
                    JSONObject jsonObject = new JSONObject(Data);
                    List<HotKeyBean> list = new ArrayList<>();
                    JSONArray array = jsonObject.getJSONArray("data");
                    if (array != null && array.length() > 0) {
                        for (int i = 0; i < array.length(); i++) {
                            HotKeyBean bannerBean = new HotKeyBean();
                            JSONObject object = array.getJSONObject(i);
                            bannerBean.setId(object.getInt("id"));
                            bannerBean.setName(object.getString("name"));
                            bannerBean.setVisible(object.getInt("visible"));
                            bannerBean.setOrder(object.getInt("order"));
                            bannerBean.setLink(object.getString("link"));
                            list.add(bannerBean);
                        }
                        LogUtils.d(TAG, list.get(0).getName());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            public Boolean onFailed(String message) {
                return null;
            }
        });
    }


}